package datasource

import (
	"fmt"
	"github.com/jinzhu/gorm"
	"mlss-cc-a/logger"
)

var DB *gorm.DB

const (
	host    = "localhost"
	port    = 3306
	db_user = "root"
	db_pass = "123456"
	db_name = "my_test"
)

func InitTestDB() {
	log := logger.Logger()
	gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
		return "t_" + defaultTableName
	}

	dbURL := fmt.Sprintf("%v:%v@(%v:%v)/%v?charset=utf8&parseTime=True&loc=Local",
		db_user, db_pass, host, port, db_name)

	log.Infof("db url: %s", dbURL)
	println(dbURL)
	db, err := gorm.Open("mysql.md", dbURL)
	if err != nil {
		log.Fatalf("failed to connect database: %v", err)
		return
	}
	db.SingularTable(true)
	DB = db
}
